package com.weishuang.monitor.leader.commons.utils;

import com.weishuang.monitor.leader.commons.enums.SCODE;
import com.weishuang.monitor.leader.config.dto.CustomUserDetails;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;

public class UserUtils {

    /**
     * 获取用户存储在Security的用户对象
     * @return
     */
    public static CustomUserDetails getUser() {
        UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        return (CustomUserDetails) authentication.getPrincipal();
    }

    /**
     * 获取用户名称
     * @return
     */
    public static String getUserName() {
        String username = getUser().getUsername();
        return username;
    }

    /**
     * 获取用户ID
     * @return
     */
    public static String getUserId() {
        String id = getUser().getId();
        return id;
    }

    /**
     * 判断用户是否管理员
     * @return
     */
    public static boolean isAdmin() {
        String isAdmin = getUser().getIsAdmin();
        return SCODE.YES_CODE.getValue().equals(isAdmin);
    }
}
